package com.custom.comm.utils;

import com.custom.comm.exceptions.CustomCheckException;

import java.util.function.Supplier;

/**
 * 断言工具类
 *
 * @author Xiao-Bai
 * @since 2022/7/21 23:58
 */
public final class AssertUtil {


    public static void isNull(Object obj, String message) {
        isTrueThrowEx(obj != null, message);
    }

    public static void isNull(Object obj) {
        isTrueThrowEx(obj != null, "The argument must not be null");
    }

    public static void notNull(Object obj, String message) {
        isTrueThrowEx(obj == null, message);
    }

    public static void notNull(Object obj) {
        isTrueThrowEx(obj == null, "The argument must be null");
    }

    public static void empty(Object obj, String message) {
        isTrueThrowEx(JudgeUtil.isNotEmpty(obj), message);
    }

    public static void empty(Object obj) {
        isTrueThrowEx(JudgeUtil.isNotEmpty(obj), "The argument must be Empty");
    }

    public static void notEmpty(Object obj, String message) {
        isTrueThrowEx(JudgeUtil.isEmpty(obj), message);
    }

    public static void notEmpty(Object obj) {
        isTrueThrowEx(JudgeUtil.isEmpty(obj), "The argument must not be Empty");
    }

    public static void npe(Object obj) {
        if (obj == null) {
            throw new NullPointerException();
        }
    }

    public static void npe(Object obj, String message) {
        if (obj == null) {
            throw new NullPointerException(message);
        }
    }

    public static void isTrueThrowEx(boolean expression, String message) {
        if (expression) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new IllegalArgumentException(message);
        }
    }

    public static <X extends Throwable> void isTrue(boolean expression, Supplier<? extends X> supplier) throws X {
        if (!expression) {
            throw supplier.get();
        }
    }

    public static void isTrue(boolean expression) {
        if (!expression) {
            throw new IllegalArgumentException("expression is not a True!");
        }
    }

    public static void illegal(boolean bool, String message) {
        isTrueThrowEx(bool, message);
    }

    public static void unSupportOp(boolean bool, String message) {
        if (bool) {
            throw new UnsupportedOperationException(message);
        }
    }

    public static void allowed(boolean bool, String message) {
        if (!bool) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void checkTrue(boolean bool, String message) {
        if (!bool) {
            throw new CustomCheckException(message);
        }
    }

    public static void checkTrue(boolean bool, String message, Object... params) {
        if (!bool) {
            throw new CustomCheckException(message, params);
        }
    }

    public static void checkNotEmpty(Object obj, String message) {
        checkTrue(JudgeUtil.isNotEmpty(obj), message);
    }

    public static <T> void assertEquals(T a, T b) {
        isTrue(a.equals(b), "a Not Equals b");
    }

    public static <T> void assertNotEquals(T a, T b) {
        isTrue(!a.equals(b), "a Not Equals b");
    }

    public static <T extends Throwable, X> void throwEx(Class<T> ex, Supplier<X> supplier) throws T {
        try {
            supplier.get();
        }catch (Exception e) {
            isTrue(ex.isInstance(e), "The exception should be an instance of " + ex +"， but not.");
        }
    }

}
